package com.androiddoc.randomgame.mycompnents;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;


public class FlashView extends View {
	
	private Context mContext;
	private Bitmap[] bitmaps;
	private int currentIdx = 0;
	private boolean start = false;
	private int randomIdx = 0;
	
	public FlashView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}

	public void initFlashBitmaps(int[] pictureIDs){
		if(pictureIDs.length > 0){
			bitmaps = new Bitmap[pictureIDs.length];
		}
		for(int i = 0; i < pictureIDs.length;i ++){
			bitmaps[i] = BitmapFactory.decodeResource(mContext.getResources(), pictureIDs[i]);
		}
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if(bitmaps == null){
			return;
		}
		if(start){
			if(currentIdx >= bitmaps.length){
				currentIdx = 0;
			}
			canvas.drawBitmap(bitmaps[currentIdx], 0, 0, null);
			currentIdx ++;
		} else {
			canvas.drawBitmap(bitmaps[randomIdx], 0, 0, null);
		}
		postInvalidateDelayed(10);
	}

	public void start(){
		start = true;
	}
	
	public void stop(int random){
		start = false;
		randomIdx = random;
	}
	
}
